import pandas as pd
import csv

apiFile = pd.read_csv("data/api_nodes_estimator.csv", encoding='utf-8')
mashFile = pd.read_csv("data/mashup_nodes_estimator.csv", encoding='utf-8')
edgeFile = pd.read_csv("data/m-a_edges.csv", encoding='utf-8')

apiDf = pd.DataFrame(apiFile)
mashDf = pd.DataFrame(mashFile)
edgeDf = pd.DataFrame(edgeFile)

mash_num_Dict = {}  # 存储mash类别出现的数量
mash_api_Dict = {}  # 二维字典，存储各mash调用不同api的数量
mash_c_Dict = {}  # 按name存储mash类别
api_c_Dict = {}  # 按url存储api类别

for mashName, mashC in zip(mashDf['name'], mashDf['c']):
    mash_c_Dict[mashName] = mashC  # 存入mash类别

for apiName, apiC in zip(apiDf['url'], apiDf['c']):
    api_c_Dict[apiName] = apiC  # 存入api类别

for mashName, apiName in zip(edgeDf['source'], edgeDf['target']):

    if not mash_c_Dict.get(mashName):  # 过滤不存在数据
        continue
    if not api_c_Dict.get(apiName):
        continue

    if mash_num_Dict.get(mash_c_Dict[mashName]):  # 存入mash出现次数
        mash_num_Dict[mash_c_Dict[mashName]] = mash_num_Dict[mash_c_Dict[mashName]] + 1
    else:
        mash_num_Dict[mash_c_Dict[mashName]] = 1

    if not mash_api_Dict.get(mash_c_Dict[mashName]):  # 定义二维字典value为字典类型
        mash_api_Dict[mash_c_Dict[mashName]] = {}

    if mash_api_Dict[mash_c_Dict[mashName]].get(api_c_Dict[apiName]):  # 存入api出现次数
        mash_api_Dict[mash_c_Dict[mashName]][api_c_Dict[apiName]] = mash_api_Dict[mash_c_Dict[mashName]][
                                                                        api_c_Dict[apiName]] + 1
    else:
        mash_api_Dict[mash_c_Dict[mashName]][api_c_Dict[apiName]] = 1

mash_api_DictDf = pd.DataFrame(mash_api_Dict)
resList = []
for x in mash_api_Dict.keys():
    # print(x, end=":")
    for y in mash_api_Dict[x].keys():
        # print(y + '(' + str(mash_api_Dict[x][y] / mash_num_Dict[x]) + ')', end=" ")
        tmp = [x, y, mash_api_Dict[x][y] / mash_num_Dict[x]]  # 计算api出现概率
        resList.append(tmp)
    # print()

file = open('result/mashup_api_by_c.csv', 'a', newline='')  # 存入文件中
writer = csv.writer(file)
for row in resList:
    writer.writerow(row)
file.close()
